﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MyMVCDemo.Models.ViewModels.Account;
using MyMVCDemo.Services;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;


namespace MyMVCDemo.Controllers
{
    public class AccountController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        private readonly AccountServices _accountServices;
        public AccountController(AccountServices accountServices)
        {
            _accountServices = accountServices;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        public IActionResult Register()
        {
            return View();
        }

        [HttpPost]

        public async Task<IActionResult> Register(RegisterModel model)
        {
            //如果校验不通过，返回原页面，带着输入值
            if (!ModelState.IsValid)
                return View(model);
            try
            {
                //存盘
                var newQq = await _accountServices.Create(model);
                return RedirectToAction(actionName: "ShowNewQQ", routeValues: new { qq = newQq });
            }
            catch (Exception e)
            {
                ModelState.AddModelError(key: "", e.Message);
                return View(model);
            }
        }

        /// <summary>
        /// 显示新的qq
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowNewQQ(string qq)
        {
            ViewBag.id = qq;
            return View();
        }


        /// <summary>
        /// 检查指定nickName是否被使用
        /// </summary>
        /// <param name="昵称"></param>
        /// <returns></returns>
        public async Task<IActionResult> CheckIfNickNameIsNotUsed(string 昵称)
        {
            if (!await _accountServices.IsNickNameUsed(昵称)) return Json(data: true);
            return Json(data: "该昵称已被使用");
        }


       



        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
       public IActionResult Login()
        {
            return View();
        }


        [HttpPost]
        public async Task<IActionResult> Login(LoginModel model)
        {
            //如果校验不通过，返回原页面，带着输入值
            if (!ModelState.IsValid)
                return View(model);
            try
            {

                //登录成功跳转
                var ans = await _accountServices.Login(model);
                if (ans)
                {
                    return RedirectToAction(actionName: "Index", controllerName: "Member");//待改
                }
                else
                {
                    ModelState.AddModelError(key: "", "用户名密码错误");
                    return View(model);
                }
            }
            catch (Exception e)
            {
                ModelState.AddModelError(key: "", e.Message);
                return View(model);
            }
        }


        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Logout()
        {
            await _accountServices.Logout();
            return RedirectToAction("Index", controllerName: "Home");
        }

    }
}
